var _QDevelop_UI_Domain = this.QDevelop_Application_Domain == null ? "/QDevelop" : this.QDevelop_Application_Domain;

var QDevelop_JsonAction = _QDevelop_UI_Domain+"/json/queryDB.action?";

var _QDevelop_UI_Debug = true;

var common = {
	phone:{type:'Phone',msg:'电话号码不正确'},	
	mobile:{type:'Mobile',msg:'手机号码不正确'}
};
/*
*	初始化某个ID Form 中的表单所有值
*	@param formId 表单ID
*	@param data 数据对象 Json对象
*	@param isQDevelopData 是否是框架获取来的数据源
*/
var setFormData = function(formId,data,isQDevelopData){
	isQDevelopData = isQDevelopData == null?false:isQDevelopData;
	var setMultiSelectValue = function(_select,value){
		_select.find('option').each(function(){
			if(value.indexOf(this.value+';')>-1){
				this.selected = true;
			}
		});
	}
	$("#"+formId).find(':input').each(function(){
		var _dValue = '';
		if(isQDevelopData)	
			_dValue = (this.name !=null && data[this.name.toUpperCase()] == null) ? data[this.id.toUpperCase()] : data[this.name.toUpperCase()];
		else
			_dValue = data[this.name] == null ? data[this.id] : data[this.name];

		if(this.type == 'radio' || this.type == 'checkbox'){
			if(_dValue!=null&&(_dValue+';').indexOf(this.value+';') > -1){
				this.checked = true;
			}
		}else if(this.type=="select-multiple"){
			if(_dValue!=null){
				setMultiSelectValue($(this),(_dValue+';'));
			}
		}else{
			$(this).val(_dValue);
		}
	});
}

/*
* 获取ID Form中表单中所有的值
* @param formId
* @return json
*/
var getFormData = function(formId){
	var tmpJson = {};
	var getMultiSelectValue = function(id,_select){
		_select.find('option').each(function(){
			if(this.selected){
				if(tmpJson[id]==null)
						tmpJson[id] = this.value;
					else
						tmpJson[id] += ';'+this.value;
			}
		});
	}
	$("#"+formId).find(':input').each(function(){
		var dataKey = (this.name == null || this.name.length == 0)?this.id:this.name;
		if(this.type == 'radio' || this.type == 'checkbox'){
			if(this.checked){
					if(tmpJson[dataKey]==null)
						tmpJson[dataKey] = this.value;
					else
						tmpJson[dataKey] += ';'+this.value;
			}
		}else if(this.type=="select-multiple"){
			getMultiSelectValue(dataKey,$(this));
		}else{
			tmpJson[dataKey] = $(this).val();
		}
		
	});
	return tmpJson;
}

/*
*	动态生成一个弹出窗口显示
*/
var QDevelopElementSequenceID = 0;//支持一个页面中多个MoreInfoView的实现

var MoreInfoView = function(infoParam,_width,_height,subMethod,noButton,isLeft){
		this.winParam = {};		
		if(typeof infoParam == 'object'){
			this.winParam = infoParam;
		}else{
			this.winParam.winId = "autoWin_"+QDevelopElementSequenceID;
			this.winParam.contentId = "autoContent_"+QDevelopElementSequenceID;
			this.winParam.content = null;
			this.winParam.width = _width;
			this.winParam.height = _height;
			this.winParam.title = infoParam.toString();
			this.winParam.isLeft = isLeft==null?false:isLeft;
			this.winParam.noButton = noButton;
			this.winParam.subMethod = subMethod;
		}

		if(this.winParam.width == null)this.winParam.width = 800;
		if(this.winParam.height == null)this.winParam.height = 400;
		if(!this.winParam.noButton)this.winParam.height = this.winParam.height<90?120:this.winParam.height;
		if(this.winParam.content!=null)this.setContent(this.winParam.content);
		this.isInited = false;
		QDevelopElementSequenceID++;

		this.init = function(){
	if(!this.isInited){
		this.isInited = true;	
		$("body").append('<div id="'+this.winParam.winId+'" title="'+this.winParam.title+'" style="display:none;overflow:hidden;width:100%;height:'+this.winParam.height+
			'"><div id="'+this.winParam.contentId+'" style="width:100%;overflow-y:scroll;overflow-x:hidden;height:'+
			(this.winParam.noButton?(this.winParam.height-10):(this.winParam.height-90))+(this.winParam.isLeft?'':';text-align:center')+';"></div></div>');
		
		if(this.winParam.subMethod){
			$("#"+this.winParam.winId).dialog({
					autoOpen: false,
					width:this.winParam.width,
					//height:this.winParam.height,
					bgiframe: true,
					modal: true,
					buttons: {				
					"  关  闭  ": function() { 
						$(this).dialog("close"); 
					},
					"  提  交  ": this.winParam.subMethod
				}
			});
		}else{
			if(this.winParam.noButton==null||!this.winParam.noButton){
				$("#"+this.winParam.winId).dialog({
						autoOpen: false,
						width:this.winParam.width,
						//height:this.winParam.height,
						bgiframe: true,
						modal: true,
						buttons: {
						"  关  闭  ": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
			}else{
				$("#"+this.winParam.winId).dialog({
						autoOpen: false,
						width:this.winParam.width,
					//	height:this.winParam.height,
						bgiframe: true,
						modal: true
				});
			}
		}
	}
}
this.setContent = function(content){
	if(typeof content == 'string')
		this.winParam.content = content;
	else{
		this.winParam.content = content.html();
		content.remove();
	}
}
this.toForm = function(formObj,validator){
	if(formObj!=null && formObj.length>0){
		if(validator!=null && typeof validator == 'object'){
			for(each in validator){
				var tar =formObj.find('* [name="'+each+'"]');
				if(tar.length==0){ 
					tar = formObj.find('* [id="'+each+'"]');
				}
				if(tar.length>0){
					var dom = $(tar.get(tar.length-1));
					var _dataType = validator[each].type;
					if(validator[each].nullAble)
						dom.attr('require','false');
					switch(_dataType){
						case "Group":
						case "Range":
						if(validator[each].min!=null)dom.attr('min',validator[each].min);
						if(validator[each].max!=null)dom.attr('max',validator[each].max);
						case "Limit":
							if(validator[each].max!=null){
								if(validator[each].min!=null){
									dom.attr({'dataType':'LimitB','min':validator[each].min,'max':validator[each].max,'msg':validator[each].msg});
								}else{
									dom.attr({'dataType':'Limit','max':validator[each].max,'msg':validator[each].msg});								
								}
							}
						default :
							if(_dataType!='Limit')
								dom.attr({'dataType':_dataType,'msg':validator[each].msg});
					}
				}
			}
		}
		this.winParam.content = formObj.html();
		formObj.remove();
	}
}
this.submit = function(vType){
	var _form = $("#"+this.winParam.winId).find('form').get(0);
	if(Validator.Validate(_form,(vType==null?2:vType))){
		_form.submit();
	}
}
this.show = function(content){
	this.init();
	$("#"+this.winParam.winId).dialog('open');
	if(content==null&&this.winParam.content != null){
			$('#'+this.winParam.contentId).html(this.winParam.content);		
	}
	if(content==null) return;
	if(typeof content == 'string'){
		$('#'+this.winParam.contentId).html(content);
	}else{
		if(this.winParam.content == null){
			this.winParam.content = content.html();
			content.remove();
		}
		$('#'+this.winParam.contentId).html(this.winParam.content);
	}
}
this.showWithData = function(data,format){
	this.show();
	if(format!=null){
		for(each in format){
			if(data[each]!=null){
				data[format[each]] = data[each];
				data[each] = null;
			}
		}
	}
	setFormData(this.winParam.winId,data,false);
}
this.showWithTable = function(data,format){
	this.show();
	if(format!=null){
		for(each in format){
			if(data[each]!=null){
				data[format[each]] = data[each];
				data[each] = null;
			}
		}
	}
	setFormData(this.winParam.winId,data,true);
}
this.toJson = function(){
	return getFormData(this.winParam.winId);
}
this.toString = function(){
	var tmpStr = '';
	var _json = this.toJson();
	for(each in _json){
		tmpStr += '&'+each+'='+_json[each];
	}
	return tmpStr.length>1?tmpStr.substring(1):'';
}
this.showHidden = function(){
	this.show();
	$("#"+this.winParam.winId).hide();
}
this.close = function(){
	$("#"+this.winParam.winId).dialog('close');
}
this.destory = function(){
	$("#"+this.winParam.winId).remove();
}

}

var wait= new MoreInfoView('数据正在提交，请稍后... ...',280,50,null,true);
wait.setContent('<IMG SRC="'+_QDevelop_UI_Domain+'/lib/image/05043110.gif">');

var Pagination = function(data,clickMethod,showDiv){
		this.paginationData = data;
		this.clickMethod = clickMethod;
		this.showDiv = showDiv;

		this.show = function(page,maxNum,_n){
	var maxShowNum = 8;
	if(this.paginationData==null || typeof this.paginationData != 'object') return;
	var minId =(page-1)*maxNum;
	var maxId = page*maxNum;
	var idx = 0;
	var tmp = '<table class="QueryTable">';
	for(each in this.paginationData){
		if(idx<=maxId && idx>=minId){
			if(idx%2==0)
				tmp += '<tr onclick="changeCss(this);'+this.clickMethod+'(\''+this.paginationData[each]+'\',\''+each+'\');"><td>'+each+'</td></tr>';
			else
				tmp += '<tr class="anthor_tr" onclick="changeCss(this);'+this.clickMethod+'(\''+this.paginationData[each]+'\',\''+each+'\');"><td >'+each+'</td></tr>';
		}
		idx ++;
	}
	tmp += '<table>';
	if(page>1 || idx>maxNum){
		var allPage = idx%maxNum==0?idx/maxNum:(1+parseInt(idx/maxNum));
		var p_temp = '';
		var nowClass = "";
		var m_1,m_2;
		var isSplit = allPage>maxShowNum?true:false;
		if(isSplit){
			m_1 = parseInt(page-(maxShowNum-2)/2);
			m_2 = parseInt(page+(maxShowNum-2)/2);
			if(m_1<2){m_2 = m_2 + Math.abs(m_1)+1;m_1 = 2;	}
			var ff = (allPage-m_2) - parseInt((maxShowNum-2)/2)  ;
			if(ff<0){m_1 +=  ff;m_2 = allPage-1;	}
		}
		var tmpP = '';
		for(var i=1;i<=allPage;i++){
			nowClass = (i==page? "hot":"common");
			if(isSplit){
				if(i==1||i==allPage||(i>=m_1&&i<=m_2)){
					p_temp += tmpP+'<a class="'+nowClass+'" href="javascript:'+_n+'.show('+i+','+maxNum+',\''+_n+'\')">'+i+'</a>&nbsp;';
					tmpP='';
				}else{
					tmpP = '...&nbsp;';
				}
			}else{
				p_temp += '<a class="'+nowClass+'" href="javascript:'+_n+'.show('+i+','+maxNum+',\''+_n+'\')">'+i+'</a>&nbsp;';
			}
		}
		tmp += '<div style="width:100%;text-align:center;">'+p_temp+'</div>';
	}
	$('#'+this.showDiv).html(tmp);
}
}

var lastObj=null;
var changeCss = function(obj){
	if(lastObj!=null){
		$(lastObj).css({'background-color':'','color':''});
	}
	$(obj).css({'background-color':'#25569E','color':'#ffffff'});
	lastObj = obj;
}


/**缓存一个页面中，多个QueryTable Param的缓存**/
var QueryTable_PARAM={};

/**
*	Ajax 获取表内容列表
**/
var QueryTable = function(param){
	var targetDom = $(param.div);
	if(targetDom.length==0){
		if(_QDevelop_UI_Debug){
			alert('未能找到属性为'+param.div+'的元素！');
		}
		return;
	}
	if(param.before){
		param.before();
	}
	targetDom.html('<div style="width:100%;text-align:center"><img src="'+_QDevelop_UI_Domain+'/lib/image/loading.gif" width="150px" height="150px"><br>正在加载数据...</div>');
	var paramKey = param.sequenceId;
	if(param.sequenceId == null || QueryTable_PARAM[param.sequenceId] == null){
		paramKey = ++QDevelopElementSequenceID;
		param.sequenceId = paramKey;
		QueryTable_PARAM[paramKey] = param;	
		QueryTable_PARAM[paramKey].defaultData = {};
		if(param.data!=null){for(each in param.data)QueryTable_PARAM[paramKey].defaultData[each] = param.data[each];}	
	}

	var cacheOrder = function(key){
		if(param.queryParam!=null){
			if(param.queryParam.indexOf('order='+key)>-1){
				if(param.queryParam.indexOf('order='+key+' desc')>-1) return -1;
				return 1;
			}
		}
		if(param.data!=null){
			if(param.data['order']!=null&&param.data['order'].indexOf(key)>-1){
				var tmp = param.data['order'].split(' ');
				if(tmp[0]!=key)return 0;
				if(tmp.length == 2) return -1;
				return 1;
			}
		}
		return 0;
	}
	
	var showOrderStat = function(title,key){
		if(key==null||key=='') return title;
		var ii = cacheOrder(key);
		switch(ii){
			case 0:return title;
			case -1: return title+'↓';
			case 1: return title+'↑';
		}
	}

	var setWidth = function(width){
		return width == null? "":" width='"+width+"'";
	}

	var _ajaxCallBack = function(jsonResult){	
	if(param.fullElemt){
		if(param.elemt==null) param.elemt = {};
		if(jsonResult!=null&&jsonResult.json!=null&&jsonResult.json.resultList&&jsonResult.json.resultList.length>0){
			var tmp = {};
			var rb = jsonResult.json.resultList[0];
			for(var each in rb){
				tmp[each]={'key':each,order:true};
			}
			var tmpParam = {};
			var formatName = function(key){
				if(param.elemtFormat!=null&&param.elemtFormat[key]!=null) return param.elemtFormat[key];
				return key;
			}			
			var insertFormParam = function(){
				for(each in tmp){
					tmpParam[formatName(each)] = tmp[each];
				}
				isFindPostion = true;
			}
			var isFindPostion = false;
			for(each in param.elemt){
				if(param.elemt[each]!=null&&param.elemt[each].key!=null){
					delete tmp[param.elemt[each].key];
				}
			}
			for(each in param.elemt){
				if(each != 'elemtFormat'){
					tmpParam[formatName(each)] = param.elemt[each];
				}else
					insertFormParam();
			}
			if(!isFindPostion)insertFormParam();
			
			param.elemt = tmpParam;
		}
	}

	var tmpHtml = '';
	tmpHtml += (param.title==null?'':param.title);

	tmpHtml += ('<div '+(param.css==null?'':'style="'+param.css+'"')+'><table id="TAB_'+paramKey+'" class="QueryTable">');
	var tmpTitle = '';
	var elemNumer = 0;
	for(each in param.elemt){
		if(param.elemt[each]!=null){
			elemNumer++;
			if(param.elemt[each].order&&param.elemt[each].key!=null)
				tmpTitle += '<th nowrap '+setWidth(param.elemt[each].width)+'><a class="QueryTable_Link" tip="点击标题可以进行排序" tipOnce href="javascript:_order(\''+paramKey+'\',\''+param.elemt[each].key.toUpperCase()+'\')">'+showOrderStat(each,param.elemt[each].key.toUpperCase())+'</a></th>';
			else
				tmpTitle += '<th nowrap '+setWidth(param.elemt[each].width)+'>'+(param.elemt[each].key==null?each:showOrderStat(each,param.elemt[each].key.toUpperCase()))+'</th>';
		}
	}
	tmpHtml += ('<tr>'+tmpTitle+'</tr>');
	if(jsonResult!=null&&jsonResult.json!=null&&jsonResult.json.resultList&&jsonResult.json.resultList.length>0){
		var _rb;
		for(var i=0;i<jsonResult.json.resultList.length;i++){
			_rb = jsonResult.json.resultList[i];
			var tmpVale = '';
			for(each in param.elemt){
				if(param.elemt[each].method){
					tmpVale += '<td >'+param.elemt[each].method(_rb[param.elemt[each].key.toUpperCase()])+'</td>';
				}else if(param.elemt[each].describe){
					tmpVale += '<td >'+param.elemt[each].describe(_rb)+'</td>';
				}else{
					var _tdText = (param.elemt[each].key==null?"":_rb[param.elemt[each].key.toUpperCase()]);
					if(_tdText!=null&&param.elemt[each].maxLen&&_tdText.length>param.elemt[each].maxLen)
						tmpVale += '<td tip="'+_tdText+'" showAllways="true" >'+_tdText.substring(0,param.elemt[each].maxLen)+'...</td>';	
					else
						tmpVale += '<td >'+_tdText+'</td>';	
				}
			}
			tmpHtml += ('<tr idx="'+i+'" '+ (i%2==0?'':'class="anthor_tr"')+'>'+tmpVale+'</tr>');
		}	
	}else{
		if(typeof jsonResult.json =='string' && jsonResult.json.indexOf('您还没有登录,请先登录;')>-1){
				//alert(jsonResult.json);
				location.href = _QDevelop_UI_Domain+"/login.action";
		}
		tmpHtml += '<tr><td colspan="'+elemNumer+'">对不起,查询暂无数据...</td></tr>';
	}
	tmpHtml += ('</table>');
	var tmpDownResultHTML = '';
	if(param.isDownResult){
		tmpDownResultHTML = '<a href="#" class="ui_downResult">下载结果</a>';
	}
	if(jsonResult!=null&&jsonResult.json!=null&&jsonResult.json.lastPage>1){
		if(typeof param.pagination == 'function'){
			var p = {};
			p.firstPage		= 1;
			p.previousPage	= jsonResult.json.previousPage;
			p.nextPage		= jsonResult.json.nextPage;
			p.lastPage		= jsonResult.json.lastPage;
			p.allCount		= jsonResult.json.allCount;
			p.nowPage		= jsonResult.json.nowPage;
			p.maxNum		= jsonResult.json.maxNum;
			tmpHtml += param.pagination(p);
		}else{
		if(param.pagination ==null || param.pagination =='CLASSIC'){
		var tmpPageOption = '';
		for(var i=1;i<=jsonResult.json.lastPage;i++){
			tmpPageOption += '<option value="'+i+'" '+(i==jsonResult.json.nowPage?'selected':'')+'>'+i+'</option>';
		}
		var tmpMaxNumOption ='<option value="'+jsonResult.json.maxNum+'">'+jsonResult.json.maxNum+'</option>';
		var _PageMaxNumCollection = [5,10,15,20,30,50,70,100];
		for(var i=0;i<_PageMaxNumCollection.length;i++){
			tmpMaxNumOption += '<option value="'+_PageMaxNumCollection[i]+'">'+_PageMaxNumCollection[i]+'</option>';
		}
		tmpHtml += '<table class="QueryTable_Pagination"><tr><td width="30%" nowrap>共'+jsonResult.json.allCount+'条记录 第'+
			jsonResult.json.nowPage+'/'+jsonResult.json.lastPage+'页</td><td width="40%" nowrap><a class="QueryTable_Link" href="javascript:_goPage(\''+paramKey+'\','+
			jsonResult.json.firstPage+')">首页</A>&nbsp;<a class="QueryTable_Link" href="javascript:_goPage(\''+paramKey+'\','+jsonResult.json.previousPage+
			')">上一页</A>&nbsp;<a class="QueryTable_Link" href="javascript:_goPage(\''+paramKey+'\','+jsonResult.json.nextPage+')">下一页</A>&nbsp;<a class="QueryTable_Link" href="javascript:_goPage(\''+paramKey+'\','+
			jsonResult.json.lastPage+')">末页</A></td><td width="30%" nowrap>转到<SELECT onchange="javascript:_goPage(\''+paramKey+'\',this.options[this.selectedIndex].value)">'+
			tmpPageOption+'</SELECT>页</td><td nowrap>每页<select onchange="javascript:_setMaxNum(\''+paramKey+'\',this.options[this.selectedIndex].value)">'+
			tmpMaxNumOption+'</select>条</td><td>'+tmpDownResultHTML+'</td></tr></table>';
		}else if(param.pagination =='PAGE_NUMBER' || param.pagination =='PAGE_NUMBER_MAXSET'){
				var maxShowNum = 5;
				var p_temp = '';
				var nowClass = "";
				var m_1,m_2;
				var isSplit = jsonResult.json.lastPage>maxShowNum?true:false;
				if(isSplit){
					m_1 = parseInt(jsonResult.json.nowPage-(maxShowNum-2)/2);
					m_2 = parseInt(jsonResult.json.nowPage+(maxShowNum-2)/2);
					if(m_1<2){m_2 = m_2 + Math.abs(m_1)+1;m_1 = 2;	}
					var ff = (jsonResult.json.lastPage-m_2) - parseInt((maxShowNum-2)/2)  ;
					if(ff<0){m_1 +=  ff;m_2 = jsonResult.json.lastPage-1;}
				}
				var tmpP = '';
				for(var i=1;i<=jsonResult.json.lastPage;i++){
					nowClass = i==jsonResult.json.nowPage? "pageHot":"pageCommon";
					if(isSplit){
						if(i==1||i==jsonResult.json.lastPage||(i>=m_1&&i<=m_2)){
							p_temp += tmpP+'<a class="'+nowClass+'" href="javascript:_goPage(\''+paramKey+'\','+i+')">'+i+'</a>';
							tmpP='';
						}else{
							tmpP = '<span>...</span>';
						}
					}else{
						p_temp += '<a class="'+nowClass+'" href="javascript:_goPage(\''+paramKey+'\','+i+')">'+i+'</a>';
					}
				}

				if(param.pagination =='PAGE_NUMBER_MAXSET'){
					p_temp += '<span>每页<input type="text" tip="修改该值,改变当前页每页显示数" class="maxSet" value="'+jsonResult.json.maxNum+'" onchange="_setMaxNum(\''+paramKey+'\',this.value)">条</span>';
					
				}
				tmpHtml += '<div class="QueryTablePage">'+p_temp+tmpDownResultHTML+'</div>';
		}
	  }
	}else if(param.isDownResult &&jsonResult.json!=null&&jsonResult.json.resultList!=null&& jsonResult.json.resultList.length>0){
		tmpHtml += '<div class="QueryTablePage">'+tmpDownResultHTML+'</div>';
	}
	targetDom.html(tmpHtml+'</div>');
	
	/**设置下载查询结果**/
	if(param.isDownResult){
		$('.ui_downResult').hover(function(){
			$(this).addClass("pageHot");
		},function(){
			$(this).removeClass("pageHot");
		});
		$('.ui_downResult').click(function(){
			if(jsonResult.json.lastPage>1&&jsonResult.json.maxNum*jsonResult.json.lastPage>10000)
				if(!confirm('您的下载数据量过大,可能需要消耗大量时间,是否确认继续下载？')) return;
			var downParam = {};
			for(each in param.data){
				downParam[each] = param.data[each];
			}		
			var _url = _QDevelop_UI_Domain+'/tranTool/DBResultDownload.action?'+param.queryParam;
			downParam.elem = toDownParam(param.elemt).replace(/\(.+?\)/g,'');
			wait.show();
			$.ajax({
				type: "GET",
				url: _QDevelop_UI_Domain+'/tranTool/DBResultDownload.action?'+param.queryParam,
				data:downParam,
				dataType: "text",
				success:function(data){
					wait.close();
					location.href = _QDevelop_UI_Domain+'/tranTool/resultDownload.action?fileName=result.csv&msg='+data;
				}	
			});
		});
	}

	if(param.pagination =='PAGE_NUMBER_MAXSET'){
		$('.maxSet').hover(function(){
			$(this).addClass("maxSetHover");
		},function(){
			$(this).removeClass("maxSetHover");
		});
	}

	if(param.click){
		$('#TAB_'+paramKey).find('TR').addClass('clickStyle');
		$('#TAB_'+paramKey).find('TR').click(function(){
			var id = $(this).attr('idx');		
			if(id!=null&&id.length>0){
				$('.TEXT_Click_CSS').removeClass('TEXT_Click_CSS');
				$(this).addClass('TEXT_Click_CSS');
				param.click(jsonResult.json.resultList[id],id);
			}
		});
	}

	if(param.bind){
		for(var each in param.bind){
			var bindDom = $('#TAB_'+paramKey).find(each);
			if(bindDom.length>0){
				for(var eAction in param.bind[each]){	
					bindDom.live(eAction,function(e){
						var _id  = $(this).closest('TR').attr('idx');
						var _tid = this.id == null?('.'+this.className):('#'+this.id);
						param.bind[_tid][e.type](jsonResult.json.resultList[parseInt(_id)]);
					});
				}			
			}
		}
	}

	if(param.behind){
		param.behind(jsonResult.json!=null?jsonResult.json.resultList:null);
	}
}
	var _queryTableUrl = param.queryURL== null?QDevelop_JsonAction:(_QDevelop_UI_Domain+param.queryURL);
	_queryTableUrl += (_queryTableUrl.indexOf('?')==-1?"?":"")+(param.queryParam==null?"":param.queryParam);
	$.ajax({
		type: "POST",
		url: _queryTableUrl+'&$r='+Math.random(),
		data: param.data,
		dataType: "json",
		cache:false,
		timeout:10000,
		success:function(jsonResult){
			_ajaxCallBack(jsonResult);
		},error:function(msg){
			_ajaxCallBack(msg);
		} 
	});
	
	this.search = function(searchData){
		if(searchData==null) return;
		var tmp = '';
		for(each in searchData){
			tmp += '&'+each+"="+encodeURIComponent(searchData[each]);
		}
		if(tmp.length>1){
			param.queryParam += tmp;
			QueryTable(param);
		}
	}
}

var	waitClose = function(){
	wait.close();
}
var _order = function(paramKey,_orderName){
	var focusUrl = QueryTable_PARAM[paramKey].queryParam;
	var data = QueryTable_PARAM[paramKey].data;
	var _isDesc = false;
	if(focusUrl!=null){	
		if(focusUrl.indexOf('order='+_orderName)>-1&&focusUrl.indexOf('order='+_orderName+' desc')>-1)_isDesc = true;
		QueryTable_PARAM[paramKey].queryParam = focusUrl.replace(/[?|\&amp;]order=([a-z|A-Z|\_|\.|\s|0-9|desc]*)/g,'');
	}
	if(data != null){
		if(data.order!=null && data.order.indexOf(_orderName+' desc')>-1)_isDesc = true;
	}else{
		QueryTable_PARAM[paramKey].data = {};
	}
	QueryTable_PARAM[paramKey].data.order = _isDesc?_orderName:(_orderName+' desc');
	QueryTable(QueryTable_PARAM[paramKey]);
}

var _goPage = function(paramKey,num){
	if(!isNumber(num)){
		alert('对不起，您输入的页数非数字，请重新输入...');
		return;
	}
	if(QueryTable_PARAM[paramKey].queryParam!=null)
		QueryTable_PARAM[paramKey].queryParam = QueryTable_PARAM[paramKey].queryParam.replace(/[?|\&amp;]page=([0-9]*)/g,'');
	if(QueryTable_PARAM[paramKey].data==null)QueryTable_PARAM[paramKey].data = {};
	QueryTable_PARAM[paramKey].data.page = num;
	QueryTable(QueryTable_PARAM[paramKey]);
}

var _setMaxNum = function(paramKey,num){
	if(!isNumber(num)){
		alert('对不起，您输入的每页显示数非数字，请重新输入...');
		return;
	}
	var focusUrl = QueryTable_PARAM[paramKey].queryParam;
	if(focusUrl!=null){
		var tmp = focusUrl.replace(/[?|\&amp;]maxNum=([0-9]*)/g,'');
		tmp = tmp.replace(/[?|\&amp;]page=([0-9]*)/g,'');
		QueryTable_PARAM[paramKey].queryParam = tmp;
	}
	if(QueryTable_PARAM[paramKey].data==null){
		QueryTable_PARAM[paramKey].data = {};
	}
	QueryTable_PARAM[paramKey].data.page=1;
	QueryTable_PARAM[paramKey].data.maxNum=num;
	QueryTable(QueryTable_PARAM[paramKey]);
}

var isNumber = function(oNum){ 
  if(!oNum) return false; 
  var strP=/^\d+(\.\d+)?$/; 
  if(!strP.test(oNum)) return false; 
  try{ 
	if(parseFloat(oNum)!=oNum) return false; 
  }catch(ex){ 
   return false; 
  } 
  return true; 
}

var _QD_Search_Query_TMP_INDEX = null;

var setSearch = function(dom,tableParam,param){
	if(QueryTable_PARAM[tableParam.sequenceId]!=null)tableParam.queryParam = QueryTable_PARAM[tableParam.sequenceId].queryParam;	
	if(tableParam.queryParam==null) tableParam.queryParam ='';
	if(tableParam.queryParam.indexOf('index')>-1)
		_QD_Search_Query_TMP_INDEX = tableParam.queryParam; 
	if(tableParam.data==null) tableParam.data={};
	parseParamData = function(key,val){	
		if(tableParam.data[key]!=null){
			tableParam.data[key] += ';'+val;
		}else
			tableParam.data[key] = val;
	}
	this.extend = function(destination,source) {
	  for(var property in source)destination[property] = source[property];
	  return destination;
	}

	if(param ==null){
		param = {};
		for(each in tableParam.elemt){
			if(each.length>0&&tableParam.elemt[each]!=null&&tableParam.elemt[each].key!=null){
				param[tableParam.elemt[each].key] = {title:each};
			}
		}
	}

	QueryTableSearch = function(){
		tableParam.data = this.extend({},QueryTable_PARAM[tableParam.sequenceId].defaultData);
		$('#searchParamTable').find(':input').each(function(){
			var _val = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
			var key = this.id.replace('@AutoId','');
			if(_val !=null && _val.length>0&&(_val.indexOf(param[key].title)==-1)){
				if(this.searchType == 'date'){
					if(param[key].method)
							param[key].method(key,_val,tableParam.data);
					else{
						if(param[key].isArea){	
							if(!validatorDate(key,_val,param[key].title))return;
							parseParamData(key,(this.isArea?">":"<")+"to_timestamp('"+_val+"','yyyy-mm-dd')");
						}else{
							parseParamData(key,"to_timestamp('"+_val+"','yyyy-mm-dd')");
						}
					}
				}
				else if(this.searchType == 'dateTimer'){
					if(param[key].method)
						param[key].method(key,_val,tableParam.data);
					else{
					if(param[key].isArea){
						if(!validatorDate(key,_val,param[key].title))return;
						parseParamData(key,(this.isArea?">":"<")+"to_timestamp('"+_val+"','yyyy-mm-dd HH24:MI:SS')");
					
					}else
						parseParamData(key,"to_timestamp('"+_val+"','yyyy-mm-dd HH24:MI:SS')");
					}
				}else if(this.searchType == 'select'){
					if(param[key].method)
						param[key].method(key,_val,tableParam.data);
					else
						parseParamData(key,_val);
				}else{
					if(param[key].method)
						param[key].method(key,_val,tableParam.data);
					else{
						if(_val.indexOf('>')==-1&&_val.indexOf('<')==-1){
							_val = _val.replace(/\*/g,'%').replace(/\s*-\s*/g,'-').replace(/\s*\+\s*/g,'+');
							_val = _val.indexOf('%')>-1?(_val.replace(/ |\+/g,'|').replace(/-/g,';!'))
								:('%'+_val.replace(/ |\+/g,'%|%').replace(/-/g,'%;!%')+'%');
							parseParamData(key,_val.replace(/%%;|;%%/g,''));
						}else{
							parseParamData(key,_val);
						}
					}
				}

			}
		});
		if(tableParam.data.index!=null)
			tableParam.queryParam = tableParam.queryParam.replace(/index=([a-z|A-Z|\_|\.]*)/g,'');
		else
			tableParam.queryParam = _QD_Search_Query_TMP_INDEX;
		QueryTable(tableParam);
	}
	
	var setValue = function(paramData,isFirst){
		if(paramData.type == null) return paramData.title == null?"":paramData.title;
		if(paramData.type == 'date' || paramData.type == 'dateTimer'){
			if(paramData.isArea){
				if(isFirst)
					return paramData.title == null?"":paramData.title+" 开始时间";
				else
					return paramData.title == null?"":paramData.title+" 结束时间";
			}
		}
		return paramData.title == null?"":paramData.title;
	}
	
	if(dom!=null&&dom.length>0&&tableParam!=null&&param!=null){		
		var tmp = '<div id="searchParamTable" class="tableSearch">';
		for(each in param){
			if(param[each].type=='date'){
				if(!param[each].isArea)
					tmp += '<input id="'+each+'" title="'+setValue(param[each])+'" value="'+setValue(param[each])+'" type="text" searchType="date" style="width:70px" onClick="WdatePicker({el:\''+each+'\',dateFmt:\'yyyy-MM-dd\'})">';
				else{
					tmp += '<input id="'+each+'"  title="'+setValue(param[each],true)+'" value="'+setValue(param[each],true)+'" type="text" searchType="date" style="width:70px" isArea=true onClick="WdatePicker({el:\''+each+'\',dateFmt:\'yyyy-MM-dd\'})">'+
						' - <input id="'+each+'@AutoId"  title="'+setValue(param[each],false)+'" value="'+setValue(param[each],false)+'" type="text" searchType="date" style="width:70px" dateTo=true onClick="WdatePicker({el:\''+each+'@AutoId\',dateFmt:\'yyyy-MM-dd\'})">';
				}
			}else if(param[each].type=='dateTimer'){
				if(!param[each].isArea)
				tmp += '<input id="'+each+'" type="text"  title="'+setValue(param[each])+'" value="'+setValue(param[each])+'" searchType="dateTimer" style="width:120px"  onClick="WdatePicker({el:\''+each+'\',dateFmt:\'yyyy-MM-dd HH:mm:ss\'})">';
				else{
					tmp += '<input id="'+each+'" type="text"  title="'+setValue(param[each],true)+'" value="'+setValue(param[each],true)+'" searchType="dateTimer" isArea=true style="width:120px"  onClick="WdatePicker({el:\''+each+'\',dateFmt:\'yyyy-MM-dd HH:mm:ss\'})">'+
						' - <input id="'+each+'@AutoId" type="text"  title="'+setValue(param[each],false)+'" value="'+setValue(param[each],false)+'" searchType="dateTimer" dateTo=true style="width:120px"  onClick="WdatePicker({el:\''+each+'@AutoId\',dateFmt:\'yyyy-MM-dd HH:mm:ss\'})">';
				}
			}else if(param[each].type=='select')
				tmp +='<select id="'+each+'" searchType="select">'+parseSelectOption(param[each])+'</select>';
			else
				tmp += '<input id="'+each+'" value="'+setValue(param[each])+'"  title="'+setValue(param[each])+'" type="text">';//tip="字符模糊查询<br>字符间加空格实现多条件组合查询<br>例：投诉 内容 <br>即查询含有[投诉]或者[内容]的记录"
			
		}
		tmp += '<a href="javascript:QueryTableSearch();" class="searchBtn" accesskey="s" title="快捷键:Alt+s">查询</a></span></div>'; 
		dom.html(tmp);
	}

	$('#searchParamTable').find('input').click(function(){
		var key = this.id.replace('@AutoId','');
		if(this.value!=null&&this.value.indexOf(param[key].title)>-1){
			this.value = '';
		}
	});

	$('#searchParamTable').find('input').blur(function(){
		var key = this.id.replace('@AutoId','');
		if(this.value==''&&param[key].title!=null){
			var isFirst = this.id.indexOf('@AutoId') == -1 ?true:false;
			this.value = setValue(param[key],isFirst);
			$(this).removeClass('inputValue');
		}else{
			$(this).addClass('inputValue');
		}
	});

	var validatorDate = function(id,value,title){
		id += '@AutoId';
		var eInput = document.getElementById(id);
		if(eInput == null||eInput.value.indexOf(title)>-1||eInput.value==value) return true;
		var dateBegin = new Date(Date.parse(value.replace(/-/g,"/")));   
		var dateEnd = new Date(Date.parse(eInput.value.replace(/-/g,"/")));   
		if(dateBegin.valueOf()>dateEnd.valueOf()) {
			alert('对不起,'+title+'结束时间必须比开始时间大!');
			eInput.value = title +' 结束时间';
			return false;
		}return true;
	}
}

var parseSelectOption = function(param){
	var tOption = '<option value="">'+(param.title==null?"":(param.title+"-"))+'全部</option>';
	if(param.option==null){
		getDB(param,function(data){		
			if(data!=null&&data.length>0){
				for(var each in data){
					var _rb = data[each];
					if(param.text!=null&&param.value!=null)
						tOption += '<option value="'+_rb[param.value.toUpperCase()]+'">'+_rb[param.text.toUpperCase()]+'</option>';
					else{
						for(each in _rb){
							tOption += '<option value="'+_rb[each]+'">'+_rb[each]+'</option>';
							break;
						}
					}
				}
				
			}
		},false);

	}else if(param.option!=null){
		if(param.option!=null){
			for(each in param.option){
				tOption += '<option value="'+each+'">'+param.option[each]+'</option>';
			}
		}
	}
	return tOption;
}


var QueryInfo = function(param){
	if(param.queryParam == null || param.elemt == null) return;
	wait.show();
	var tmpHtml = '';
	getDB(param,function(resultList){
		if(resultList==null||resultList.length==0)return;
		tmpHtml += ('<div '+(param.css==null?'':'style="'+param.css+'"')+'><table class="QueryTable">');
		var _rb = resultList[0];
		var rowIdx = 0;
		for(each in param.elemt){
			tmpHtml += '<tr '+(rowIdx%2==0?'':'class="anthor_tr"')+'><td nowrap>'+each+'</td>';
			if(param.elemt[each].method)
				tmpHtml += '<td>'+param.elemt[each].method(_rb[param.elemt[each].key.toUpperCase()])+'</td>';
			else if(param.elemt[each].describe)
				tmpHtml += '<td>'+param.elemt[each].describe(_rb)+'</td>';
			else 
				tmpHtml += '<td>'+_rb[param.elemt[each].key.toUpperCase()]+'</td>';
			tmpHtml += '</tr>';
			rowIdx++;
		}
		$(param.div).html(tmpHtml+'</table></div>');
	});
}

/*
var _QDevelop_UI_Tip_Sequence = 100;
var _tipAutoCloseTimer;
var Tip = function(param){
	if(param.width == null)param.width=200;
	if(param.height == null)param.height=150;
	var tipDom = $('#Tip_'+_QDevelop_UI_Tip_Sequence);

	this.close = function(){
		$('#Tip_'+_QDevelop_UI_Tip_Sequence).fadeOut('slow');
	}

	if(tipDom.length==0){
		_QDevelop_UI_Tip_Sequence++;
		var tip_Model = '<div id="Tip_'+_QDevelop_UI_Tip_Sequence+'" style="display:none;position:absolute;top:0px;left:0px;width:'+
		'200px;heigth:150px;filter:alpha(opacity=80);cursor:default;z-index:'+(_QDevelop_UI_Tip_Sequence)+'">'+
		'<div style="width:100%;heigth:15px;background:transparent url('+_QDevelop_UI_Domain+'/lib/image/layer_top.gif) top;padding-bottom:1px">&nbsp;</div>'+
		'<div id="pan" style="width:100%;height:'+(param.height-15)+'px;background-color:#F3F8FE;border-left:1px solid #8BB5E0;border-right:1px solid #8BB5E0;border-bottom:1px solid #8BB5E0;">'+
		'<div id="closeBar" title="关闭提示" style="cursor:pointer;width:11px;heigth:11px;float:right;background:transparent url('+_QDevelop_UI_Domain+'/lib/image/tab-close.gif)  no-repeat top;"></div>'+
		'<div id="tip_content" style="padding-top:3px;padding-left:2px;"></div></div>'+
		'<div id="tipAccess" style="width:100%;text-align:center;background-color:#8BB5E0;height:18px;font-size:11px;border:1px;padding-bottom:3px"><input type="checkbox">知道了,下次不再提示</div></div>';	
		$("body").append(tip_Model);
		tipDom = $('#Tip_'+_QDevelop_UI_Tip_Sequence);
		if(tipDom.length==0) return;
		tipDom.find('#closeBar').click(function(){
			$(this).parent().parent().fadeOut('slow');
		});
		tipDom.find('#pan').mouseover(function(){
			if(_tipAutoCloseTimer!=null)
				window.clearTimeout(_tipAutoCloseTimer);
		});
		tipDom.find('#pan').mouseout(function(){
			_tipAutoCloseTimer = window.setTimeout(close,3000);
		});
	}

	if(_tipAutoCloseTimer!=null) window.clearTimeout(_tipAutoCloseTimer);
	var _toShow = function(){		
		tipDom.css({'left':(param.left-(param.width*0.39)),'top':param.top,'width':param.width,'height':param.height});
		tipDom.find('#tip_content').html(param.content);
		if(param.tipAccess){
			tipDom.find('#tipAccess').show();
			tipDom.find(':checkbox').attr('checked',false);
		}else{
			tipDom.find('#tipAccess').hide();
		}
		tipDom.fadeIn('slow');
	}
	window.setTimeout(_toShow,10);
	_tipAutoCloseTimer = window.setTimeout(close,3000);
}

QDevelop_UI_Tip_width = 150;	QDevelop_UI_Tip_height = 70;

$.expr[':'].withTip = function(obj){var $this = $(obj);  return ($this.attr('tip')!=null);};
$('body').find(':withTip').live('mouseover',function(e){
	var _html = $(this).attr('tip');
	if(_html!=null && _html.length>0){
		var showAllways = $(this).attr('showAllways');
		if(showAllways!=null && showAllways == 'true'){
			Tip({width:QDevelop_UI_Tip_width,height:QDevelop_UI_Tip_height,top:e.pageY,left:e.pageX,content:_html});
		}else{
			if(getCookie(_html) != '-'){ 
				Tip({width:QDevelop_UI_Tip_width,height:QDevelop_UI_Tip_height,top:e.pageY,left:e.pageX,content:_html,tipAccess:true});
				if($(this).attr('tipOnce')!=null)
					setCookie(_html,'-');
			}
		}	
	}
});

$('div[id^="Tip_"]').find(':checkbox').live('click',function(){
	var key = $('div[id^="Tip_"]').find('#tip_content').html();
	if(this.checked){
		setCookie(key,'-');
	}else{
		setCookie(key,null);
	}
});

*/
var setCookie = function(key,value){
	var Days = 30; //此 cookie 将被保存 30 天
	var exp  = new Date();    //new Date("December 31, 9998");
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie =  key+"="+ escape (value) + ";expires=" + exp.toGMTString();
}

var getCookie =function(name){
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	 if(arr != null) return unescape(arr[2]); return "[]";
}

var getDB = function(param,method,isAsync,isMulti){
	var _queryTableUrl;
	if(param.queryURL!=null){
		_queryTableUrl = _QDevelop_UI_Domain+param.queryURL+"?";
	}else{
		_queryTableUrl =_QDevelop_UI_Domain+"/json/"+(isMulti?"multiQueryDB":"queryDB")+".action?";
	}
	_queryTableUrl += param.queryParam==null?"":param.queryParam;
	_queryTableUrl += "&$r="+Math.random()+(param.index==null?"":"&index="+param.index)
	$.ajax({
		type: "POST",
		url: _queryTableUrl,
		data:param.data,
		dataType: "json",
		cache:false,
		async:(isAsync==null?true:isAsync),
		timeout:10000,
		success:function(data){
			wait.close();
			method(data.json.resultList);
		},error:function(){
			wait.close();
			method({});
		}
	});
}

var queryDB = function(param,method,isAsync){
	if(typeof param == 'string'){
		getDB({data:{'index':param}},method,isAsync);
	}else{
		getDB({data:param},method,isAsync);
	}
}
var ajaxQuery = function(param,method){
	wait.show();
	if(typeof method == 'function'){
		if(typeof param == 'string'){
			getDB({data:{'index':param}},method);
		}else{
			getDB(param,method,param.isAsync);
		}
	}
}

var ajaxUpdate = function(param){
	updateDB(param.data,param.success,param.isMulti,param.queryURL);
}

var updateDB = function(data,method,isMulti,queryURL){
	var _queryTableUrl;
	if(queryURL!=null){
		_queryTableUrl = _QDevelop_UI_Domain+queryURL+"?";
	}else{
		_queryTableUrl =_QDevelop_UI_Domain+"/json/"+(isMulti?"multiUpdateDB":"updateDB")+".action?";
	}
	wait.show();
	$.ajax({
		type: "POST",
		url: _queryTableUrl+"&$r="+Math.random(),
		data:data,
		cache:false,
		dataType: "json",
		success:function(jsonResult){
			wait.close();
			method(jsonResult);
		},error:function(){
			wait.close();
			alert('Ajax Update DB Error...');
		}
	});
}

var toDownParam = function(p){
	var tmp = [];
	var idx=0;
	for(each in p){
		if(p[each].key!=null&&p[each].key.length>0)
			tmp.push('"'+each+'":{key:"'+p[each].key+'",idx:'+(idx++)+'}');
	}
	return tmpStr = '{' + tmp.join(',') + '}';
}

var toString = function(o){
	var r = [];
    if(typeof o =="string") return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
    if(typeof o =="undefined") return "undefined";
	//if(typeof o == "function") return "''";
    if(typeof o == "object"){
        if(o===null) return "null";
        else if(!o.sort){
            for(var i in o)
                r.push(i+":"+toString(o[i]))
            r="{"+r.join()+"}"
        }else{
            for(var i =0;i<o.length;i++)
                r.push(toString(o[i]))
            r="["+r.join()+"]"
        }
        return r;
    }
    return o.toString();
}